﻿using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InfoCollect;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductsInfoCollectItemService
    {
        public InsuranceProductsInfoCollectItem LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public bool IsExist(string infoCollectItemName, int infoCollectCategoryID, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductsInfoCollectItem>();
            if (!string.IsNullOrEmpty(infoCollectItemName))
            {
                expressionWhere = expressionWhere.And(o => o.InfoCollectItemName.Equals(infoCollectItemName));
            }
            if (infoCollectCategoryID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.InfoCollectCategoryID == infoCollectCategoryID);
            }
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }
        public bool Delete(int id)
        {
            var entity = LoadByID(id);
            if (entity != null)
            {
                return DeleteEntities(entity);
            }
            return false;
        }
        public IQueryable<InsuranceProductsInfoCollectItem> GetPagingList(InsuranceProductsInfoCollectItemModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductsInfoCollectItem>();
            if (model.InfoCollectCategoryID.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.InfoCollectCategoryID == model.InfoCollectCategoryID.Value);
            }
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => o.InfoCollectItemName.Contains(model.Keyword));
            }
            return LoadPagerEntities<InsuranceProductsInfoCollectItem>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }

       
    }
}